package com.ele.client.activity.terminal;

import com.ele.client.activity.terminal.widget.ImagesWidget;
import com.ele.client.activity.terminal.widget.QuestionWidget;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.QuoteElement;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.*;

import java.util.List;

public class TerminalViewImpl extends Composite implements TerminalView {

    interface DefaultUiBinder extends UiBinder<Widget, TerminalViewImpl> {
    }

    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    @UiField
    FlowPanel wordsContainer;
    @UiField
    ImagesWidget imagesWidget;
    @UiField
    HTML descriptionHtml;
    @UiField
    QuestionWidget questionWidget;
    @UiField
    Button button;
    @UiField
    InputElement textField;
    @UiField
    HTMLPanel examplesPanel;
    @UiField
    QuoteElement errorText;

    private Presenter presenter;

    public TerminalViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void setWordWidgets(List<IsWidget> widgets) {
        wordsContainer.clear();
        for (IsWidget widget : widgets) {
            wordsContainer.add(widget);
        }
    }

    @Override
    public void setDescriptionText(String text) {
        descriptionHtml.setHTML(text);
        descriptionHtml.setVisible(text != null && !text.isEmpty());
    }

    @Override
    public void appendDescriptionText(String text) {
        descriptionHtml.setHTML(descriptionHtml.getHTML() + text);
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public ImagesWidget getImagesWidget() {
        return imagesWidget;
    }

    @Override
    public void setQuestionText(String text) {
        questionWidget.setText(text);
    }

    @Override
    public void setAnswerExamples(List<String> validAnswers) {
        examplesPanel.getElement().setInnerText("");
        for (String answer : validAnswers) {
            Element small = DOM.createElement("small");
            small.setInnerText(answer);
            examplesPanel.getElement().appendChild(small);
        }
        errorText.getStyle().setDisplay(Style.Display.BLOCK);
    }

    @Override
    public void setAnswerSuccess() {
        errorText.getStyle().setDisplay(Style.Display.NONE);
    }

    @Override
    public void clearAnswer() {
        textField.setValue("");
        examplesPanel.getElement().setInnerText("");
    }

    @UiHandler("button")
    public void onButtonClick(ClickEvent event) {
        presenter.checkAnswer(textField.getValue());
    }
}
